/*
 * Copyright (C) 2020 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xui_lib.component.button.shinebutton;

import ohos.agp.components.AttrSet;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Matrix;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * 图标资源控件
 *
 * @author xuexiang
 * @since 2020-01-06
 */
public class PorterShapeImageView extends PorterImageView {
    private Element mIconDrawable;
    private Element mIconDrawableSelect;
//    private PixelMap iconPixelMapNoSelect;
    private Matrix mMatrix;

    /**
     * PorterShapeImageView
     *
     * @param context
     */
    public PorterShapeImageView(Context context) {
        this(context, null);
    }

    /**
     * PorterShapeImageView
     *
     * @param context
     * @param attrs
     */
    public PorterShapeImageView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    /**
     * PorterShapeImageView
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public PorterShapeImageView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs, defStyleAttr);
    }

    private void initAttrs(Context context, AttrSet attrs, int defStyleAttr) {
        if (attrs != null) {
            mIconDrawable = attrs.getAttr("sb_icon_image").get().getElement();
            mIconDrawableSelect = attrs.getAttr("sb_icon_select_image").get().getElement();
            PixelMap pixelMap = ((PixelMapElement) mIconDrawable).getPixelMap();
            setScaleMode(ScaleMode.STRETCH);
            setPixelMap(pixelMap);
        }
        mMatrix = new Matrix();
    }

    /**
     * setTintDrawable
     *
     * @param isSlect
     */
    protected void setTintDrawable(boolean isSlect) {
        Element nowIconDrawable = isSlect ? mIconDrawableSelect : mIconDrawable;
        PixelMap pixelMap = ((PixelMapElement) nowIconDrawable).getPixelMap();
        setScaleMode(ScaleMode.STRETCH);
        setPixelMap(pixelMap);
    }

    /**
     * 设置图标资源
     *
     * @param drawable
     */
    public void setIconDrawable(Element drawable) {
        mIconDrawable = drawable;
        invalidate();
    }

    /**
     * 设置选中状态的图标资源
     *
     * @param drawable
     */
    public void setSelectIconDrawable(Element drawable) {
        mIconDrawableSelect = drawable;
        invalidate();
    }

    @Override
    protected Element paintMaskCanvas(Canvas maskCanvasss, Paint maskPaintss, int widths, int heights) {
        if (mIconDrawable != null) {
            if (mIconDrawable instanceof PixelMapElement) {

                configureBitmapBounds(getWidth(), getHeight());
            }

            mIconDrawable.setBounds(0, 0, getWidth(), getHeight());
            return mIconDrawable;
        }
        return null;
    }

    private void configureBitmapBounds(int viewWidth, int viewHeight) {
        int drawableWidth = mIconDrawable.getWidth();
        int drawableHeight = mIconDrawable.getHeight();
        boolean fits = viewWidth == drawableWidth && viewHeight == drawableHeight;

        if (drawableWidth > 0 && drawableHeight > 0 && !fits) {
            mIconDrawable.setBounds(0, 0, drawableWidth, drawableHeight);
            float widthRatio = (float) viewWidth / (float) drawableWidth;
            float heightRatio = (float) viewHeight / (float) drawableHeight;
            float scale = Math.min(widthRatio, heightRatio);
            float dx = (int) ((viewWidth - drawableWidth * scale) * 0.5f + 0.5f);
            float dy = (int) ((viewHeight - drawableHeight * scale) * 0.5f + 0.5f);

            mMatrix.setScale(scale, scale);
            mMatrix.postTranslate(dx, dy);
        }
    }
}
